package tau_sadna.tau_map;

import org.mapsforge.map.android.view.MapView;

import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LayerButton extends LinearLayout {

	private TauLayer layer;
	private MapView mainActivity;
	private TextView button;
	private CheckBox checkbox;

	public LayerButton(MainActivity activity, TauLayer l) {
		super(activity);
		button = new TextView(activity);
		checkbox = new CheckBox(activity);
		layer = l;
		mainActivity = activity.mapView;
		
		LayoutParams left = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		LayoutParams right = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		left.weight = 1;
		button.setTextSize(18);
		button.setText(layer.getName().getName());
		button.setLayoutParams(left);
		checkbox.setLayoutParams(right);
		checkbox.setGravity(Gravity.RIGHT);
		addView(button);
		addView(checkbox);
		
		layer.setOnDisplayChangeListener(new DisplayChangeListener() {

			@Override
			public void onAdd(TauLayer layer, MapView view) {
				LayerButton.this.setCheckbox(true);
			}

			@Override
			public void onRemove(TauLayer layer, MapView view) {
				LayerButton.this.setCheckbox(false);
			}
			
		});
		
		OnClickListener clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (layer.isDislplayed)
					layer.removeFrom(mainActivity);
				else
					layer.addTo(mainActivity);
			}
		};
		checkbox.setOnClickListener(clickListener);
		setOnClickListener(clickListener);
	}

	protected void setCheckbox(boolean checked) {
		checkbox.setChecked(checked);
	}

}
